<template>
  <form-item-wrapper
    :designer="designer"
    :field="field"
    :rules="rules"
    :design-state="designState"
    :parent-widget="parentWidget"
    :parent-list="parentList"
    :index-of-parent-list="indexOfParentList"
    :sub-form-row-index="subFormRowIndex"
    :sub-form-col-index="subFormColIndex"
    :sub-form-row-id="subFormRowId"
  >
    <el-descriptions 
    :title="options.descriptions.title" 
    :border="options.descriptions.border"
    :direction="options.descriptions.direction"
    :colon="options.descriptions.colon"
    :column="options.descriptions.column"
    >
      <el-descriptions-item
        v-for="item in describeList"
        :key="item.label"
        :label="item.label"
        >{{ item.value }}</el-descriptions-item
      >
    </el-descriptions>
  </form-item-wrapper>
</template>
<script>
import FormItemWrapper from "./form-item-wrapper";
import emitter from "@/utils/emitter";
import i18n, { translate } from "@/utils/i18n";
import fieldMixin from "@/components/form-designer/form-widget/field-widget/fieldMixin";
import { EventBus } from "@/components/javaScript/EventBus";

export default {
  name: "descriptions-widget",
  componentName: "DescriptionsWidget",
  mixins: [emitter, fieldMixin, i18n],
  props: {
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
  },
  components: {
    FormItemWrapper,
  },
  inject: ["refList", "formConfig", "globalOptionData", "globalModel"],
  data() {
    return {
      fieldModel: [],
      rules: [],
    };
  },
  computed: {
    options() {
      return this.field.options;
    },
    describeList() {
      const code = this.field.options.descriptions.code;
      return JSON.parse(code) || [];
    },
  },
  beforeCreate() {
    /* 这里不能访问方法和属性！！ */
  },

  created() {
    /* 注意：子组件mounted在父组件created之后、父组件mounted之前触发，故子组件mounted需要用到的prop
           需要在父组件created中初始化！！ */
    this.initFieldModel();
    this.registerToRefList();
    this.initEventHandler();
    this.buildFieldRules();

    this.handleOnCreated();
  },
  mounted() {
    let that = this;
    this.handleOnMounted();
    this.setTransferData(this.dataSourceMapData);

    EventBus.$on(this.options.dataSource, (data) => {
      that.setTransferData(data);
    });
  },

  beforeDestroy() {
    this.unregisterFromRefList();
  },
  methods: {
    setTransferData(v) {
      if (this.options.dataSource && v && v[this.options.dataSource]) {
        this.$set(this, "transferData", v[this.options.dataSource]);
      }
    },
  },
};
</script>
<style lang="scss" scoped>
@import "../../../../styles/global.scss";
</style>
